/*本模块功能:权重设置
        编写者:朱光曜
        编写时间:2017.11.03*/
$(function() {
    var string1 = '<tr>' +
        '<th class="col-lg-2">序号</th>' +
        '<th class="col-lg-8">课程名称</th>' +
        '<th class="col-lg-5">操作</th>' +

        '</tr>';
    var box1 = new Confirm;
    var box = new Alert;
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
        loading1.show();

    });
    $(document).ajaxStop(function(){
        loading1.hide();

    });

    document.addEventListener('DOMSubtreeModified',function(e){//去除空格
        $(".publicText").attr("maxlength","20");


        $.map($(".publicText"),function (opt) {//长度限制
            maxLengthLimit.init(opt);
        });
        $(".publicText").bind('input propertychange',function(){

            var regy = /\s/g;
            if(regy.test($(this).val())){
                $(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
            }
        });
    });
    function weightValue() { //权重值设置
        return '<div class="weightValue">' +
            '<p class="popup_contenthead">权重值设置<span class="fr closes"><img src="pc/images/ico_alertDel.png" alt="叉" /></span></p>' +
            '<div class="clear"></div>' +
            '<div class="pop_main">' +
            '<div class="inputRight"><label for="departNames"  class="fl">权重:&ensp;</label>' +
            '<select id="selectWeight" class="publicSelect fl" >' +
            '<option value="0.1">1</option>' +
            '<option value="0.2">2</option>' +
            '<option value="0.3">3</option>' +
            '<option value="0.4">4</option>' +
            '<option value="0.5">5</option>' +
            '<option value="0.6">6</option>' +
            '<option value="0.7">7</option>' +
            '<option value="0.8">8</option>' +
            '<option value="0.9">9</option>' +
            '</select>' +
            '</div>' +
            '<div class="clear"></div>' +
            '<div class="prompt">' +
            '<span class="fl">提示:&ensp;</span>' +
            '<ul class="fl">' +
            '<li>1.（每个同学当前科目平时分*（100/当前班级当前科目最高平时分））*0.权重=平时成绩换算值考试成绩*（1-0.权重）=考试成绩换算值</li>' +
            '<li>2. 默认值：4</li>' +
            '</ul>' +
            '</div>' +
            '<div class="clear"></div>' +
            '</div>' +
            '<div class="down">' +
            '<div class="publicBtn preservation">保存</div>' +
            '</div>' +
            '</div>';
    }

    function setWeightValue() { //评价分设置
        return '<div class="setWeightValue">' +
            '<p class="popup_contenthead">评价分设置<span class="fr closes"><img src="pc/images/ico_alertDel.png" alt="叉" /></span></p>' +
            '<div class="clear"></div>' +
            '<div class="pop_main">' +
            '<div class="sex" id="sex"><span>评分设置:&ensp;</span>'+
            '<span class="positionArea">' +
            '<div class="bubble"> <span></span> <em></em><p class="active">开启时,评价分为选择优、良、中、差、自动评分</p></div>'+
            '<input type="radio" checked="checked" name="sex" id="man" value="" /><label for="man" class="checked" num="0">开启</label>'+
            '</span>'+
            '<span class="positionArea">' +
            '<div class="bubble"> <span></span> <em></em><p class="active">关闭时,评分为手动输入评分</p></div>'+
            '<input type="radio" name="sex" id="woman" value="" /><label for="woman" num="1">关闭</label>'+
            '</span>'+
            '</div>'+
            '<input value="100" class="publicText fl" id="selectExcellent">'+
            '<input value="90" class="publicText fl" id="selectGood">'+
            '<input value="80" class="publicText fl" id="selectIn">'+
            '<input value="60" class="publicText fl" id="selectDifference">'+
            '<div class="clear"></div>' +
            '<div class="down">' +
            '<div class="publicBtn preservation">保存</div>' +
            '</div>' +
            '</div>';
    }

    function tabelModel(data) {//列表内容
        //var time = new Date(data.courseList[key].problenTime); formatDate(time)
        return '<tr data-num="0" courseId="' + data.courseList[key].courseId + '">' +
            '<td class="col-lg-2">' + ((parseInt(key) + 1)+(data.pageNow-1)*10) + '</td>' +
            '<td class="col-lg-10" title="' + data.courseList[key].courseName + '">' + data.courseList[key].courseName + '</td>' +
            '<td class="col-lg-6" >' +
            '<a href="javascript:;" class="weight">权重值设置</a>' +
            '<a href="javascript:;" class="score">评价设置</a>' +
            '</td>' +

            '</tr>';

    }
    var nowData = 1;
    var index = {
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            this.data_content = {}; //搜索集
            this.tab_content = {}; //列表集
            this.parameter = {
                "pageNow": 1,
                "pageSize": 10
            };

            this.postData(this.parameter, nowData);
            $(".funBox .searchBtn").off("click").click(function() { //搜索点击



                var keyW = $("#keyWord").val();
                if(verification.specialCode(keyW) !== true) {
                    self.alert1("不能输入特殊字符(@/'\"#$%&^*)");
                    return;
                }
                var nowData = 1;
                var parameter = {
                    "courseName": keyW,

                    "pageNow": 1,
                    "pageSize": 10
                };
                var j=0;
                //遍历删除空元素
                for(i in parameter){
                    if(j==0){j++;continue;}
                    if(parameter[i]==null||parameter[i]==""){
                        delete parameter[i];
                    }
                    if(!isNaN(parameter[i])){
                        parameter[i]=Number(parameter[i]);
                    }
                }


                self.postData(parameter, nowData, 1);
            });
            $(document).off("click","#sex .positionArea").on("click","#sex .positionArea",function(){

                $(this).children("label").addClass("checked").parents(".positionArea").siblings(".positionArea").children("label").removeClass("checked");
                if($(this).children("input").attr("id")=="man"){
                    $("#Popup .setScore").css("display","block");
                }else{
                    $("#Popup .setScore").css("display","none");
                }
            });
        },
        postData: function(par_data, nowData, hunt) { //获得搜索及列表数据
            var self = this;
            var parameter = par_data;

            $.ajax({
                type: "post",
                url: "getCourseNoEndListByTeaIdByPost",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    var new_data = JSON.parse(data);
                    loading1.hide();
                    if(new_data.result == "success"){
                        self.data_content = new_data;
                        self.tab_content = new_data;

                        self.tab_add();

                        // console.log(new_data.totalPage+"啊啊"+new_data.pageNow)
                        var sumpage = new_data.totalPage;
                        if(sumpage<1){sumpage=1;};
                        var nowpage = 1;
                        var pagenumber = 10;
                        var parame = parameter;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                if(!parameter) {
                                    parameter = {
                                        "pageNow": nowpage,
                                        "pageSize": 10
                                    };

                                } else {
                                    parameter.pageNow = nowpage;

                                }
                                $.ajax({
                                    type: "post",
                                    url: "getCourseNoEndListByTeaIdByPost",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        //获取院系
                                        var msgNew = JSON.parse(msg);

                                        self.data_content = msgNew;
                                        self.tab_content = msgNew;


                                        self.tab_add();

                                    },
                                    error: function(data, status, xhr) {
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });
                    }else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
                    }


                },
                error: function(data, status, xhr) {
                    loading1.hide();
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },

        tab_add: function() { //列表追加

            var self = this;
            var data = self.tab_content;
            var table = string1;

            //data.selectList={};

            var i = 0;
            for(key in data.courseList) {
                i++;
            }

            if(i > 0) {

                for(key in data.courseList) {

                    table += tabelModel(data);
                }

                $(".mainContent .tableBox").html(table);
            } else {
                var trImg = "<tr><td colspan='3'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
                $(".mainContent .tableBox").html(string1 + trImg);
            }

            //设置列表宽度(最大为9)
            var numMax = 0;
            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));
                numMax += num1;
            });

            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));

                $(this).css("width", num1 / numMax * 100 + "%");
            });





            $(document).off("click", "#Popup .closes").on("click", "#Popup .closes", function() { //关闭弹窗
                $("#Popup").css("display", "none").html("");
                $("#popupBackground").css("display","none");
            });
            $(".weight").off("click").click(function(){//权重点击
                var $_self=self;
                $("#Popup").css("display","block").html(weightValue());
                $("#popupBackground").css("display","block");
                var courseId=parseInt($(this).parents("tr").attr("courseId"));
                var parameter={
                    "courseId":courseId
                };

                $.ajax({
                    type: "post",
                    url: "getSourceSetByTeaIdOrCourseIdByPost",
                    async: true,
                    data: {
                        "jsonStr": JSON.stringify(parameter)
                    },
                    async: true,
                    success: function(msg) {
                        //获取院系
                        var msgNew = JSON.parse(msg);

                        if(msgNew.sorceMap){
                            $("#selectWeight option").eq(msgNew.sorceMap.classPerformance*10-1).attr("selected","selected");

                        }else{
                            $("#selectWeight option").eq(3).attr("selected","selected");

                        }

                        $(".preservation").off("click").click(function() {//权重保存

                            if(msgNew.sorceMap){
                                var souceMark=0;
                                var parameter = {
                                    "courseId": courseId,
                                    "classPerformance":parseFloat( $("#selectWeight option:selected").val()),
                                    "bad":msgNew.sorceMap.bad,
                                    "excellent": msgNew.sorceMap.excellent,
                                    "good": msgNew.sorceMap.good,
                                    "inso": msgNew.sorceMap.inso,
                                    "souceMark":msgNew.sorceMap.souceMark
                                };
                            }else{
                                var parameter = {
                                    "courseId": courseId,
                                    "classPerformance": parseFloat($("#selectWeight option:selected").val()),
                                    "bad":1,
                                    "excellent": 4,
                                    "good": 3,
                                    "inso": 2,
                                    "souceMark":0
                                };
                            }



                            $_self.sub(parameter);
                        });

                    },
                    error: function(data, status, xhr) {
                        var json = JSON.stringify({
                            "pageName": "error"
                        });
                        window.location.href = "skipPage?jsonStr=" + json;

                    }
                });
            });
            $(".score").off("click").click(function() {//评分点击
                var $_self=self;
                $("#Popup").css("display","block").html(setWeightValue());
                $("#popupBackground").css("display","block");
                var courseId=$(this).parents("tr").attr("courseId");
                var parameter = {
                    "courseId": parseInt(courseId)
                };

                $.ajax({
                    type: "post",
                    url: "getSourceSetByTeaIdOrCourseIdByPost",
                    async: true,
                    data: {
                        "jsonStr": JSON.stringify(parameter)
                    },
                    success: function(msg) {
                        //获取院系
                        var msgNew = JSON.parse(msg);

                        if(msgNew.sorceMap){
                            if(msgNew.sorceMap.souceMark==1){
                                $("#sex label").eq(1).addClass("checked").parents(".positionArea").siblings(".positionArea").children("label").removeClass("checked");
                                $("#Popup .setScore").css("display","none");
                            }
                            if(msgNew.sorceMap.souceMark==0){
                                $("#sex label").eq(0).addClass("checked").parents(".positionArea").siblings(".positionArea").children("label").removeClass("checked");
                                $("#Popup .setScore").css("display","block");
                            }
                            // $("#selectExcellent option").eq(msgNew.sorceMap.excellent - 1).attr("selected", "selected");
                            // $("#selectGood option").eq(msgNew.sorceMap.good - 1).attr("selected", "selected");
                            // $("#selectIn option").eq(msgNew.sorceMap.inso  - 1).attr("selected", "selected");
                            // $("#selectDifference option").eq(msgNew.sorceMap.bad  - 1).attr("selected", "selected");
                            $("#selectExcellent").val(msgNew.sorceMap.excellent);
                            $("#selectGood").val(msgNew.sorceMap.good);
                            $("#selectIn").val(msgNew.sorceMap.inso);
                            $("#selectDifference").val(msgNew.sorceMap.bad);

                        }else{
                            // $("#selectExcellent option").eq(3).attr("selected", "selected");
                            // $("#selectGood option").eq(2).attr("selected", "selected");
                            // $("#selectIn option").eq(1).attr("selected", "selected");
                            // $("#selectDifference option").eq(0).attr("selected", "selected");
                            $("#selectExcellent").val("100");
                            $("#selectGood").val("90");
                            $("#selectIn").val("80");
                            $("#selectDifference").val("60");

                        }
                        $(".preservation").off("click").click(function(){//保存点击
                            if(msgNew.sorceMap){

                                var souceMark=0;
                                var parameterSor = {
                                    "courseId": courseId,
                                    "classPerformance":msgNew.sorceMap.classPerformance,
                                    "bad":parseInt($("#selectDifference").val()),
                                    "excellent":parseInt($("#selectExcellent").val()),
                                    "good":parseInt($("#selectGood").val()),
                                    "inso":parseInt($("#selectIn").val()),
                                    "souceMark":parseInt($("#sex .checked").attr("num"))
                                };
                            }else{
                                var parameterSor = {
                                    "courseId": courseId,
                                    "classPerformance":0.4,
                                    "bad":parseInt($("#selectDifference").val()),
                                    "excellent":parseInt($("#selectExcellent").val()),
                                    "good":parseInt($("#selectGood").val()),
                                    "inso":parseInt($("#selectIn").val()),
                                    "souceMark":parseInt($("#sex .checked").attr("num"))
                                };
                            }
                            if($("#sex .checked").html()=="开启"){


                                if(parameterSor){
                                    if(parameterSor.excellent<parameterSor.good||parameterSor.excellent==parameterSor.good){
                                        $_self.alert1("评价分设置有误,请重新设置");
                                        return;
                                    }else if(parameterSor.good<parameterSor.inso||parameterSor.good==parameterSor.inso){
                                        $_self.alert1("评价分设置有误,请重新设置");
                                        return;
                                    }else if(parameterSor.inso<parameterSor.bad||parameterSor.inso==parameterSor.bad){
                                        $_self.alert1("评价分设置有误,请重新设置");
                                        return;
                                    }
                                }
                            }
                            $_self.sub(parameterSor,courseId);
                        });
                    },
                    error: function(data, status, xhr) {
                        var json = JSON.stringify({
                            "pageName": "error"
                        });
                        window.location.href = "skipPage?jsonStr=" + json;

                    }
                });
            });

        },
        sub:function(parameter,courseId){//保存方法
            var self=this;

            $("#Popup").css("display","none").html("");
            $("#popupBackground").css("display","none");

            $.ajax({
                type: "post",
                url: "addSorceSet",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                async: true,
                success: function(msg) {
                    //获取院系
                    var msgNew = JSON.parse(msg);

                    if(msgNew.result=="success"){
                        self.alert1("保存成功");
                    }else{
                        //self.alert1(msgNew.errMsg);
                        if(msgNew.errMsg){
                            self.alert1(msgNew.errMsg);
                        }else{
                            self.alert1("提交失败");
                        }
                        $("#Popup").css("display","block").html(setWeightValue());
                        $("#popupBackground").css("display","block");
                        var parameter = {
                            "courseId": parseInt(courseId)
                        };

                        $.ajax({
                            type: "post",
                            url: "getSourceSetByTeaIdOrCourseIdByPost",
                            async: true,
                            data: {
                                "jsonStr": JSON.stringify(parameter)
                            },
                            async: true,
                            success: function(msg) {
                                //获取院系
                                var msgNew = JSON.parse(msg);

                                if(msgNew.sorceMap){
                                    if(msgNew.sorceMap.souceMark==1){
                                        $("#sex label").eq(1).addClass("checked").parents(".positionArea").siblings(".positionArea").children("label").removeClass("checked");
                                        $("#Popup .setScore").css("display","none");
                                    }
                                    if(msgNew.sorceMap.souceMark==0){
                                        $("#sex label").eq(0).addClass("checked").parents(".positionArea").siblings(".positionArea").children("label").removeClass("checked");
                                        $("#Popup .setScore").css("display","block");
                                    }
                                    // $("#selectExcellent option").eq(msgNew.sorceMap.excellent - 1).attr("selected", "selected");
                                    // $("#selectGood option").eq(msgNew.sorceMap.good - 1).attr("selected", "selected");
                                    // $("#selectIn option").eq(msgNew.sorceMap.inso  - 1).attr("selected", "selected");
                                    // $("#selectDifference option").eq(msgNew.sorceMap.bad  - 1).attr("selected", "selected");
                                    $("#selectExcellent").val(msgNew.sorceMap.excellent);
                                    $("#selectGood").val(msgNew.sorceMap.good);
                                    $("#selectIn").val(msgNew.sorceMap.inso);
                                    $("#selectDifference").val(msgNew.sorceMap.bad);

                                }else{
                                    // $("#selectExcellent option").eq(3).attr("selected", "selected");
                                    // $("#selectGood option").eq(2).attr("selected", "selected");
                                    // $("#selectIn option").eq(1).attr("selected", "selected");
                                    // $("#selectDifference option").eq(0).attr("selected", "selected");
                                    $("#selectExcellent").val("100");
                                    $("#selectGood").val("90");
                                    $("#selectIn").val("80");
                                    $("#selectDifference").val("60");

                                }
                                $(".preservation").off("click").click(function(){//保存点击
                                    if(msgNew.sorceMap){

                                        var souceMark=0;
                                        var parameterSor = {
                                            "courseId": courseId,
                                            "classPerformance":msgNew.sorceMap.classPerformance,
                                            "bad":parseInt($("#selectDifference").val()),
                                            "excellent":parseInt($("#selectExcellent").val()),
                                            "good":parseInt($("#selectGood").val()),
                                            "inso":parseInt($("#selectIn").val()),
                                            "souceMark":parseInt($("#sex .checked").attr("num"))
                                        };
                                    }else{
                                        var parameterSor = {
                                            "courseId": courseId,
                                            "classPerformance":0.4,
                                            "bad":parseInt($("#selectDifference").val()),
                                            "excellent":parseInt($("#selectExcellent").val()),
                                            "good":parseInt($("#selectGood").val()),
                                            "inso":parseInt($("#selectIn").val()),
                                            "souceMark":parseInt($("#sex .checked").attr("num"))
                                        };
                                    }
                                    if($("#sex .checked").html()=="开启"){


                                        if(parameterSor){
                                            if(parameterSor.excellent<parameterSor.good||parameterSor.excellent==parameterSor.good){
                                                self.alert1("评价分设置有误,请重新设置");
                                                return;
                                            }else if(parameterSor.good<parameterSor.inso||parameterSor.good==parameterSor.inso){
                                                self.alert1("评价分设置有误,请重新设置");
                                                return;
                                            }else if(parameterSor.inso<parameterSor.bad||parameterSor.inso==parameterSor.bad){
                                                self.alert1("评价分设置有误,请重新设置");
                                                return;
                                            }
                                        }
                                    }
                                    self.sub(parameterSor,courseId);
                                });
                            },
                            error: function(data, status, xhr) {
                                var json = JSON.stringify({
                                    "pageName": "error"
                                });
                                window.location.href = "skipPage?jsonStr=" + json;

                            }
                        });
                    }

                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        alert1: function(content1) { //alert提示

            box.contenthead = "提示";
            box.content = content1;

            box.success = function() {
                box.hide();
            };


            box.init();
            box.show();
        }

    };
    index.init();

});